const isMobile = (value) => {
  const mobileReg = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/
  return mobileReg.test(value)
}

function debounce(func, wait = 500, immediate = true) {
  let timeout

  return function () {
    let context = this
    let args = arguments
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      var callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) func.apply(context, args)
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args)
      }, wait)
    }
  }
}

function throttle(func, wait = 500, type = 1) {
  let previous = 0
  let timeout
  return function () {
    let context = this
    let args = arguments
    if (type === 1) {
      let now = Date.now()
      if (now - previous > wait) {
        func.apply(context, args)
        previous = now
      }
    } else if (type === 2) {
      if (!timeout) {
        timeout = setTimeout(() => {
          timeout = null
          func.apply(context, args)
        }, wait)
      }
    }
  }
}
const Storage = {
  set(key, data, time) {
    localStorage.setItem(
      key,
      JSON.stringify({
        data: data,
        ctime: new Date().getTime(),
        express: time,
      })
    )
  },
  get(key) {
    const { data, ctime, express } = JSON.parse(localStorage.getItem(key))
    const curTime = new Date().getTime()
    if (curTime - ctime >= express) {
      localStorage.removeItem(key)
      return null
    }
    return data
  },
}

export default {
  isMobile,
  debounce,
}
